package com.vilinno.tlias.controller;

import com.vilinno.tlias.pojo.Clazz;
import com.vilinno.tlias.pojo.ClazzQueryParam;
import com.vilinno.tlias.pojo.PageResult;
import com.vilinno.tlias.pojo.Result;
import com.vilinno.tlias.service.ClazzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    private static final Logger log = LoggerFactory.getLogger(ClazzController.class);

    @Autowired
    private ClazzService clazzService;

    /**
     * 添加班级
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz){

        log.info("添加的班级信息为:{}",clazz);
        clazzService.add(clazz);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id){

        log.info("根据id删除班级信息:{}",id);
        clazzService.delete(id);
        return Result.success();

    }

    /**
     * 修改班级信息
     * @param clazz
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("添加班级：{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级信息
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级信息");
        List<Clazz> clazzList= clazzService.findAll();
        return Result.success(clazzList);
    }

    /**
     * 分页查询
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){

        log.info("分页查询班级信息:{}",clazzQueryParam);
        PageResult<Clazz> pageResult=clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result searchById(@PathVariable("id") Integer id){

        log.info("根据id查询班级:{}",id);
        Clazz clazz=clazzService.searchById(id);
        return Result.success(clazz);

    }

}
